package com.macbulldev.poc.testgin.domain;

import java.util.Date;
import java.util.List;
import javax.persistence.Column;
import javax.persistence.EntityManager;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import javax.persistence.Version;

import org.apache.log4j.Logger;
import org.hibernate.validator.constraints.NotEmpty;
import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.transaction.annotation.Transactional;


@Configurable
@javax.persistence.Entity
@org.springframework.roo.addon.javabean.RooJavaBean
@org.springframework.roo.addon.tostring.RooToString
@org.springframework.roo.addon.entity.RooEntity
public class TransactionQuestion {
	
	private static final Logger LOGGER = Logger.getLogger(TransactionQuestion.class);
	
    private String Question;

    private String Answer;

    private String CreateUsername;

    private String UpdateUsername;
    
    private String Type;

    @javax.persistence.Temporal(javax.persistence.TemporalType.TIMESTAMP)
    @org.springframework.format.annotation.DateTimeFormat(style = "S-")
    private java.util.Date CreateTs;

    @javax.persistence.Temporal(javax.persistence.TemporalType.TIMESTAMP)
    @org.springframework.format.annotation.DateTimeFormat(style = "S-")
    private java.util.Date UpdateTs;
    
    @javax.persistence.ManyToOne(targetEntity = com.macbulldev.poc.testgin.domain.StateOrProvince.class)
    @javax.persistence.JoinColumn(name="stateOrProvinceId")
    private com.macbulldev.poc.testgin.domain.StateOrProvince stateOrProvince;
    
    @javax.persistence.ManyToOne(targetEntity = com.macbulldev.poc.testgin.domain.TrialGroup.class)
    @javax.persistence.JoinColumn(name="trialGroupId")
    private com.macbulldev.poc.testgin.domain.TrialGroup trialGroup;
    
    @javax.persistence.ManyToOne(targetEntity = com.macbulldev.poc.testgin.domain.LiabilityAmount.class)
    @javax.persistence.JoinColumn(name="liabilityAmountId")
    private com.macbulldev.poc.testgin.domain.LiabilityAmount liabilityAmount;

	@PersistenceContext    
    transient EntityManager entityManager;

	@Id    
    @GeneratedValue(strategy = GenerationType.AUTO)    
    @Column(name = "id")    
    private Long id;

	@Version    
    @Column(name = "version")    
    private Integer version;

	public Long getId() {    
        return this.id;        
    }

	public void setId(Long id) {    
        this.id = id;        
    }

	public Integer getVersion() {    
        return this.version;        
    }

	public void setVersion(Integer version) {    
        this.version = version;        
    }

	@Transactional    
    public void persist() {    
        if (this.entityManager == null) this.entityManager = entityManager();        
        this.entityManager.persist(this);        
    }

	@Transactional 
    public void remove() {    
        if (this.entityManager == null) this.entityManager = entityManager();        
        if (this.entityManager.contains(this)) {        
            this.entityManager.remove(this);            
        } else {        
            TransactionQuestion attached = this.entityManager.find(TransactionQuestion.class, this.id);  
            LOGGER.error("Why the fuck won't this delete????:  "+attached.getId()+"  :  "+attached.getQuestion());
            this.entityManager.remove(attached);            
        }        
    }

	@Transactional    
    public void flush() {    
        if (this.entityManager == null) this.entityManager = entityManager();        
        this.entityManager.flush();        
    }

	@Transactional    
    public void merge() {    
        if (this.entityManager == null) this.entityManager = entityManager();        
        TransactionQuestion merged = this.entityManager.merge(this);        
        this.entityManager.flush();        
        this.id = merged.getId();        
    }

	public static final EntityManager entityManager() {    
        EntityManager em = new TransactionQuestion().entityManager;        
        if (em == null) throw new IllegalStateException("Entity manager has not been injected (is the Spring Aspects JAR configured as an AJC/AJDT aspects library?)");        
        return em;        
    }

	public static long countTransactionQuestions() {    
        return (Long) entityManager().createQuery("select count(o) from TransactionQuestion o").getSingleResult();        
    }
	
	public static long countTransactionQuestionsForStates() {    
        return (Long) entityManager().createQuery("select count(o) from TransactionQuestion o where o.stateOrProvince.code != ''").getSingleResult();        
    }

	public static List<TransactionQuestion> findAllTransactionQuestions() {    
        return entityManager().createQuery("select o from TransactionQuestion o").getResultList();        
    }

	public static TransactionQuestion findTransactionQuestion(Long id) {    
        if (id == null) throw new IllegalArgumentException("An identifier is required to retrieve an instance of TransactionQuestion");        
        return entityManager().find(TransactionQuestion.class, id);        
    }

	public static List<TransactionQuestion> findTransactionQuestionEntries(int firstResult, int maxResults) {    
        return entityManager().createQuery("select o from TransactionQuestion o").setFirstResult(firstResult).setMaxResults(maxResults).getResultList();        
    }
	public static List<TransactionQuestion> findAllTransactionQuestionEntriesByTransaction(int firstResult, int maxResults) {    
        return entityManager().createQuery("select o from TransactionQuestion o where upper(trim(o.Type))=upper(trim('transaction'))").setFirstResult(firstResult).setMaxResults(maxResults).getResultList();        
    }
	public static List<TransactionQuestion> findTransactionQuestionsForStates(int firstResult, int maxResults){
		return entityManager().createQuery("select o from TransactionQuestion o where o.stateOrProvince.code != '' order by o.stateOrProvince.code asc").setFirstResult(firstResult).setMaxResults(maxResults).getResultList();
	}
	
	public static List<TransactionQuestion> findTransactionQuestionsForStatesByTypeTransaction(StateOrProvince state){
		return entityManager().createQuery("select o from TransactionQuestion o where upper(trim(o.Type))=upper(trim('transaction')) and o.stateOrProvince.id = ?").setParameter(1, state.getId()).getResultList();
	}
	public static List<TransactionQuestion> findTransactionQuestionsForStatesByTypePolicy(StateOrProvince state){
		return entityManager().createQuery("select o from TransactionQuestion o where upper(trim(o.Type))=upper(trim('policy')) and o.stateOrProvince.id = ?").setParameter(1, state.getId()).getResultList();
	}
	public static List<TransactionQuestion> findTransactionQuestionsForStatesByTypeEndorsement(StateOrProvince state){
		return entityManager().createQuery("select o from TransactionQuestion o where upper(trim(o.Type))=upper(trim('endorsement')) and o.stateOrProvince.id = ?").setParameter(1, state.getId()).getResultList();
	}
	
	@Transactional
	public static void deleteTransactionQuestionByTrialGroup(TransactionQuestion t)
	{
		EntityManager entityManager = new TransactionQuestion().entityManager;
		String hql = "delete from TransactionQuestion t where upper(trim(t.Question))=upper(trim(?)) and t.trialGroup.id = ?";
		Query q = null;
		
		try{
    		
    		q = entityManager.createQuery(hql);
    		
    	}
    	catch(Exception ex)
    	{
    		LOGGER.error("Error:  ",ex);
    	}
    	
    	q.setParameter(1, t.getQuestion());
    	q.setParameter(2, t.getTrialGroup().getId());
		q.executeUpdate();
	}
	
	public static TransactionQuestion findTransactionQuestionByQuestionAndState(String question, StateOrProvince state) {
    	
    	
    	EntityManager entityManager = new TransactionQuestion().entityManager;
    	
    	String hql = "select r from TransactionQuestion r where upper(trim(r.Question))=upper(trim(?)) and r.stateOrProvince.id = ?";
    	
    	
    	Query q = null;
    	try{
    		
    		q = entityManager.createQuery(hql);
    		
    	}
    	catch(Exception ex)
    	{
    		LOGGER.error("Error:  ",ex);
    	}
    	
    	
    	q.setParameter(1, question);
    	q.setParameter(2, state.getId());
    	
    	TransactionQuestion retObj = null;
    	
    	try{
    		retObj = (TransactionQuestion)q.getSingleResult();
    	}catch(Exception ex)
    	{
    		LOGGER.debug("query error:  "+ex);
    	}
    	
        return retObj;
    }
	
	

	public String getQuestion() {    
        return this.Question;        
    }

	public void setQuestion(String Question) {    
        this.Question = Question;        
    }

	public String getAnswer() {    
        return this.Answer;        
    }

	public void setAnswer(String Answer) {    
        this.Answer = Answer;        
    }

	public String getCreateUsername() {    
        return this.CreateUsername;        
    }

	public void setCreateUsername(String CreateUsername) {    
        this.CreateUsername = CreateUsername;        
    }

	public String getUpdateUsername() {    
        return this.UpdateUsername;        
    }

	public void setUpdateUsername(String UpdateUsername) {    
        this.UpdateUsername = UpdateUsername;        
    }

	public String getType() {    
        return this.Type;        
    }

	public void setType(String Type) {    
        this.Type = Type;        
    }

	public Date getCreateTs() {    
        return this.CreateTs;        
    }

	public void setCreateTs(Date CreateTs) {    
        this.CreateTs = CreateTs;        
    }

	public Date getUpdateTs() {    
        return this.UpdateTs;        
    }

	public void setUpdateTs(Date UpdateTs) {    
        this.UpdateTs = UpdateTs;        
    }

	public StateOrProvince getStateOrProvince() {    
        return this.stateOrProvince;        
    }

	public void setStateOrProvince(StateOrProvince stateOrProvince) {    
        this.stateOrProvince = stateOrProvince;        
    }

	public TrialGroup getTrialGroup() {    
        return this.trialGroup;        
    }

	public void setTrialGroup(TrialGroup trialGroup) {    
        this.trialGroup = trialGroup;        
    }
	
	public LiabilityAmount getLiabilityAmount() {    
        return this.liabilityAmount;        
    }

	public void setLiabilityAmount(LiabilityAmount liabilityAmount) {    
        this.liabilityAmount = liabilityAmount;        
    }

	public String toString() {    
        StringBuilder sb = new StringBuilder();        
        sb.append("Id: ").append(getId()).append(", ");        
        sb.append("Version: ").append(getVersion()).append(", ");        
        sb.append("Question: ").append(getQuestion()).append(", ");        
        sb.append("Answer: ").append(getAnswer()).append(", ");        
        sb.append("CreateUsername: ").append(getCreateUsername()).append(", ");        
        sb.append("UpdateUsername: ").append(getUpdateUsername()).append(", ");        
        sb.append("Type: ").append(getType()).append(", ");        
        sb.append("CreateTs: ").append(getCreateTs()).append(", ");        
        sb.append("UpdateTs: ").append(getUpdateTs()).append(", ");        
        sb.append("StateOrProvince: ").append(getStateOrProvince()).append(", ");        
        sb.append("TrialGroup: ").append(getTrialGroup());        
        return sb.toString();        
    }
}
